package org.lanqiao.api.common;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Properties;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.lanqiao.common.Constants;
import org.lanqiao.common.JsonResult;
import org.lanqiao.common.JsonResultWriter;
import org.lanqiao.util.HttpUtil;
import org.lanqiao.util.RandomUtil;
import org.lanqiao.util.SqlUtil;

/**
 * Servlet implementation class IndustrySMSServlet
 */
@WebServlet(description = "这是一个短信验证码的接口", urlPatterns = { "/api/static/SMS" })
public class IndustrySMSServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    private static String operation = "/industrySMS/sendSMS";
    
    private static Properties properties =new Properties();
    //加载配置文件,load  类加载器
    static {
        try {
            properties.load(SqlUtil.class.getClassLoader().getResourceAsStream("config.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static String accountSid = properties.getProperty("ACCOUNT_SID");
//    private static String to = "15534373826";
//    private static String smsContent = "【昭坪科技】您的验证码为362731，请于30分钟内正确输入，如非本人操作，请忽略此短信。";	
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndustrySMSServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		   
	       String to=request.getParameter("telphone");
	       //创建一个JsonResult封装消息，然后转为json，变成一个对象{}
	       JsonResult result=null;
	       String code=RandomUtil.randomNum(6);
	       if(to==null||to=="") {
	           result=new JsonResult ("短信号码未填写",Constants.STATUS_UNFOUND);
	       }else {
	           String tmpSmsContent = null;
	           
               String smsContent = "【昭坪科技】您的验证码为"+code+"，请于30分钟内正确输入，如非本人操作，请忽略此短信。";
	            try{
	              tmpSmsContent = URLEncoder.encode(smsContent, "UTF-8");
	            }catch(Exception e){
	              e.printStackTrace();
	            }
	            
	            String url = properties.getProperty("BASE_URL") + operation;
	            String body = "accountSid=" + accountSid + "&to=" + to + "&smsContent=" + tmpSmsContent
	                + HttpUtil.createCommonParam();

	            // 提交请求
	            String backresult = HttpUtil.post(url, body);
	            result=new JsonResult<>("发送成功",Constants.STATUS_SUCCESS,code);
	            
	            System.out.println("result:" + System.lineSeparator() + backresult);
	       }
	     //将响应和结果  数据，转为json
	     JsonResultWriter.writer(response, result);  
	}

}
